﻿using CompanyManageSystem.DLL;
using CompanyManageSystem.Model;
using CompanyManageSystem.Service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CompanyManageSystem.UI.Info
{
    //公司添加，修改界面
    public partial class Frm_AddCompany : BaseXtraForm
    {
        #region 属性
        public Guid? ID { get; set; }

        /// <summary>
        /// 保存执行事件
        /// </summary>
        public event EventHandler SaveEvent;

        #endregion

        #region 构造方法
        /// <summary>
        /// 构造方法
        /// </summary>
        public Frm_AddCompany(EStatus status,Guid? id = null)
        {
            InitializeComponent();
            this.FStatus = status;
            this.ID = id;
        }

        #endregion

        #region 窗体加载完成
        /// <summary>
        /// 窗体加载完成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Frm_AddCompany_Load(object sender, EventArgs e)
        {
            switch (FStatus)
            {
                case EStatus.Add:
                    this.Text = "公司 - 添加";
                    break;
                case EStatus.Modify:
                    AppService aservice = new AppService();
                    Company com = aservice.GetCompanyByID(this.ID);
                    CName.Text = com.CName;
                    Code.Text = com.Code;
                    IsEnabled.Checked = com.IsEnabled;
                    this.Text = "公司 - 修改";
                    break;
                                   
            }

        }
        #endregion

        #region 按钮事件
        /// <summary>
        /// 确定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_OK_Click(object sender, EventArgs e)
        {
            AppService aservice = new AppService();
            Company com = null;
            if (this.FStatus == EStatus.Add)
            {
                com = aservice.AddCompany(this.ID, CName.Text.Trim(), Code.Text.Trim(), this.IsEnabled.Checked);
                this.Hide();
                MessageBox.Show("添加成功!");
            }
            else if (this.FStatus == EStatus.Modify)
            {
                com = aservice.UpdateCompany(this.ID, CName.Text.Trim(),Code.Text.Trim(), IsEnabled.Checked);
                this.Hide();
                MessageBox.Show("修改成功");
            }
            if (SaveEvent != null)
            {
                SaveEvent(com,new EventArgs());
            }
            this.Close();
        }

        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_Cancle_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

    }
}
